//
//  SetPaypwdKeyboard.m
//  WaywardPoint_Code
//
//  Created by 韩盼盼 on 15/10/29.
//  Copyright © 2015年 易网新科技. All rights reserved.
//

#import "SetPaypwdKeyboard.h"
#import "CustomNumerKeyboard.h"
#define PayCount 6


@interface SetPaypwdKeyboard ()<CustomNumerKeyboardDelegate>

@property(nonatomic,strong)NSMutableArray *payArr;

@end

@implementation SetPaypwdKeyboard
- (NSMutableArray *)payArr
{
    if (!_payArr) {
        _payArr=[NSMutableArray array];
    }
    return _payArr;
}
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.bounds = CGRectMake(0, 0, ScreenW, ScreenH);
//        self.backgroundColor=RXDColor(241, 241, 241);
        self.hidden=NO;
        
        CGFloat btnH=50;
        
        //支付密码的框
        UIView *pwdview=[[UIView alloc]initWithFrame:CGRectMake(8, 50, ScreenW-16, 50)];
        pwdview.layer.masksToBounds=YES;
        pwdview.layer.borderWidth=0.8;
        CGFloat btnW=pwdview.width/6;
        pwdview.layer.borderColor=RXDColor(241, 241, 241).CGColor;
        pwdview.backgroundColor=[UIColor whiteColor];
        [self addSubview:pwdview];
        
        for (int i=0; i<6; i++) {
            UITextField *pwdla=[[UITextField alloc]initWithFrame:CGRectMake(i*btnW, 0, btnW, btnH)];
            pwdla.textAlignment=NSTextAlignmentCenter;
            pwdla.secureTextEntry=YES;
            //        pwdla.userInteractionEnabled=NO;
            pwdla.enabled=NO;
            pwdla.keyboardType=UIKeyboardTypeNumberPad;
            pwdla.font=[UIFont systemFontOfSize:20];
            pwdla.layer.masksToBounds=YES;
            pwdla.layer.borderWidth=0.8;
            pwdla.layer.borderColor=RXDColor(241, 241, 241).CGColor;
            
            //        pwdla.backgroundColor=RXDRandomColor;
            pwdla.tag=i+300;
            [pwdview addSubview:pwdla];
        }
        
        
        UIView *keyboardview=[[UIView alloc]initWithFrame:CGRectMake(0, ScreenH-248, ScreenW, 216)];
        keyboardview.backgroundColor=[UIColor blueColor];
        [self addSubview:keyboardview];
        
        CustomNumerKeyboard *keyboard = [[CustomNumerKeyboard alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 216)];
        [keyboardview addSubview:keyboard];
        //    keyboard.hidden=NO;
        //    self.textField.inputView = keyboard;
        //    self.textField.enabled=NO;
        keyboard.delegate = self;
        
        
        
    }
    return self;
}
#pragma mark - CustomNumerKeyboardDelegate
-(void)changeKeyboardType
{
    [self.delegate closedKeyboard];
}

-(void)numberKeyboardBackspace
{
    [self clearpaypwd];
}

-(void)numberKeyboardInput:(NSInteger)number
{
    if (self.payArr.count< PayCount) {
        [self.payArr addObject:[NSString stringWithFormat:@"%d",(int)number]];
        [self getpaypwd:self.payArr];
    }
}

- (NSString *)getpaypwd:(NSArray *)pwdstrarr
{
    
    NSString *paypwdstr=@"";
    UITextField *pwdT1=(UITextField *)[self viewWithTag:300];
    UITextField *pwdT2=(UITextField *)[self viewWithTag:301];
    UITextField *pwdT3=(UITextField *)[self viewWithTag:302];
    UITextField *pwdT4=(UITextField *)[self viewWithTag:303];
    UITextField *pwdT5=(UITextField *)[self viewWithTag:304];
    UITextField *pwdT6=(UITextField *)[self viewWithTag:305];
    //    [pwdT6 addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    
    if (pwdT1.text.length==0) {
        pwdT1.text=pwdstrarr[0];
    }else if (pwdT2.text.length==0)
    {
        pwdT2.text=pwdstrarr[1];
    }else if (pwdT3.text.length==0)
    {
        pwdT3.text=pwdstrarr[2];
    }else if (pwdT4.text.length==0)
    {
        pwdT4.text=pwdstrarr[3];
    }else if (pwdT5.text.length==0)
    {
        pwdT5.text=pwdstrarr[4];
    }else if (pwdT6.text.length==0)
    {
        pwdT6.text=pwdstrarr[5];
    }
    if (pwdT1.text.length!=0&&pwdT2.text.length!=0&&pwdT3.text.length!=0
        &&pwdT4.text.length!=0&&pwdT5.text.length!=0&&pwdT6.text.length!=0) {
        paypwdstr=[NSString stringWithFormat:@"%@%@%@%@%@%@",pwdT1.text,pwdT2.text,pwdT3.text,pwdT4.text,pwdT5.text,pwdT6.text];
     
        [self.delegate getKeyboardInput:paypwdstr];
        
        
    }
    
    return  paypwdstr;
}
- (void)clearpaypwd
{
    UITextField *pwdT1=(UITextField *)[self viewWithTag:300];
    UITextField *pwdT2=(UITextField *)[self viewWithTag:301];
    UITextField *pwdT3=(UITextField *)[self viewWithTag:302];
    UITextField *pwdT4=(UITextField *)[self viewWithTag:303];
    UITextField *pwdT5=(UITextField *)[self viewWithTag:304];
    UITextField *pwdT6=(UITextField *)[self viewWithTag:305];
    if (pwdT6.text.length!=0) {
        pwdT6.text=@"";
        [self.payArr removeLastObject];
    }else if (pwdT5.text.length!=0)
    {
        pwdT5.text=@"";
        [self.payArr removeLastObject];
    }else if (pwdT4.text.length!=0)
    {
        pwdT4.text=@"";
        [self.payArr removeLastObject];
    }else if (pwdT3.text.length!=0)
    {
        pwdT3.text=@"";
        [self.payArr removeLastObject];
    }else if (pwdT2.text.length!=0)
    {
        pwdT2.text=@"";
        [self.payArr removeLastObject];
    }else if (pwdT1.text.length!=0)
    {
        pwdT1.text=@"";
        [self.payArr removeLastObject];
    }
    
}

@end
